package com.eeepay.eeepay_v2.activity;

import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.QRCodeAdapter;
import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.ConfigPorperties;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;
import com.eeepay.eeepay_v2.view.QRCodeDialog;
import com.eeepay.v2_library.log.LogUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.util.ImageLoaderUtils;
import com.eeepay.v2_library.view.HorizontalItemView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import cn.eeepay.api.grpc.nano.AppInfo2ApiServiceGrpc;
import cn.eeepay.api.grpc.nano.AppInfo2Proto;
import cn.eeepay.api.grpc.nano.QRcodeApiServiceGrpc;
import cn.eeepay.api.grpc.nano.QRcodeProto;
import cn.eeepay.api.grpc.nano.ReqBaseProto;
import io.grpc.ManagedChannel;

/**
 * Created by Administrator on 2017/1/5.
 */
public class QRCodeActivity2 extends ABBaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private ListView listView;
    private QRCodeAdapter adapter;
    private QRCodeDialog qrCodeDialog;

    private List<AppInfo2Proto.AppInfo2> datas;
    private AppInfo2Proto.AppInfo2 qRcode;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_qr_code2;
    }

    @Override
    protected void initWidget() {
        listView = getViewById(R.id.lv_qr_code);
    }

    @Override
    protected void eventOnClick() {
        listView.setOnItemClickListener(this);
        adapter = new QRCodeAdapter(this);
        sendHttpRequest(0);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        qRcode = (AppInfo2Proto.AppInfo2) parent.getAdapter().getItem(position);
        if (qrCodeDialog == null) {
            qrCodeDialog = new QRCodeDialog(mContext);
            qrCodeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
        qrCodeDialog.show();
        qrCodeDialog.setName(qRcode.appName);
        qrCodeDialog.setCodeUrl(qRcode.codeUrl);
    }

    @Override
    protected void sendHttpRequest(int taskId) {
        showProgressDialog();
        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT, taskId, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                dismissProgressDialog();
                AppInfo2ApiServiceGrpc.AppInfo2ApiServiceBlockingStub stub = AppInfo2ApiServiceGrpc.newBlockingStub(channel);
                ReqBaseProto.ReqTwoStr request = new ReqBaseProto.ReqTwoStr();
                request.param1 = ConfigPorperties.getInstance().getAppNo();
                request.param2 = ConfigPorperties.getInstance().getTeamId();
                return stub.getAppInfo(request);
            }

            @Override
            public void updateUI(Object result, int taskId) {
                dismissProgressDialog();
                if (result == null) {
                    showToast("查询失败");
                    return;
                }
                AppInfo2Proto.AppInfo2List response = (AppInfo2Proto.AppInfo2List) result;
                if (!response.success) {
                    showToast(response.msg);
                    return;
                }
                if (response.appInfo2 != null && response.appInfo2.length > 0) {
                    datas = Arrays.asList(response.appInfo2);
                    adapter.addAll(datas);
                    listView.setAdapter(adapter);
                }
            }
        });
    }

}
